﻿using System;
using System.Collections.Generic;

namespace RexToy.Cassandra
{
    class DateType : IMarshal
    {
        private static DateTime Epoc = new DateTime(1970, 1, 1);
        public byte[] GetBytes(object data)
        {
            if (data == null)
                return Constant.Zero_Length_Byte_Array;
            DateTime d = (DateTime)data;
            var span = d - Epoc;
            var bytes = BitConverter.GetBytes((long)span.TotalMilliseconds);

            MarshalUtil.AdjustBufferOrder(bytes);
            return bytes;
        }

        public object GetData(byte[] bytes)
        {
            if (bytes.Length == 0)
                return null;

            MarshalUtil.AdjustBufferOrder(bytes);

            long l = BitConverter.ToInt64(bytes, 0);
            TimeSpan s = new TimeSpan(l * 10000);
            return Epoc + s;
        }        
    }
}
